package com.dx.chat.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description:
 *
 * @author rockstarsteve
 * @version 1.0
 * @copyright Copyright (c) 电信
 * @since 2023/9/8
 */
public class AdressUtil {

    /**
     * 判断字符串是否是正确的ip地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIP(String ipAddr) {
        if (ipAddr == null || "".equals(ipAddr) || ipAddr.length() < 7 || ipAddr.length() > 15) {
            return false;
        }
        /**
         * 判断IP格式是否正确
         */
        String rexp = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[0-9])\\.((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.){2}(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pat = Pattern.compile(rexp);
        Matcher mat = pat.matcher(ipAddr);
        boolean ipAddress = mat.find();
        return ipAddress;
    }

    /**
     * 判断断开格式是否正确
     *
     * @param port
     * @return
     */
    public static boolean isPort(String port) {
        if (port == null || "".equals(port)) {
            return false;
        }
        String rexp = "^\\d{1,5}$";
        return port.matches(rexp) && Integer.parseInt(port) >= 1 && Integer.parseInt(port) <= 65535;
    }
}
